package LeetCode1;

//对称二叉树
public class Num101 {
    //答案
//    public boolean isSymmetric(TreeNode root) {
//        if (root == null) {
//            return true;
//        }
//        return check(root,root);
//    }
//
//    private boolean check(TreeNode p, TreeNode q) {
//        if (p == null && q == null) {
//            return true;
//        }
//        if (p == null || q == null) {
//            return false;
//        }
//        return p.val == q.val && check(p.left,q.right) && check(p.right,q.left);
//    }
    //课堂
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isMrror(root.left, root.right);
    }

    private boolean isMrror(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null) {
            return false;
        }
        return isSame(left.left, right.right) && isSame(left.right, right.left);
    }

    private boolean isSame(TreeNode p, TreeNode q) {
        // 边界条件
        if (p == null && q == null) {
            // 两棵树都是空树
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        return p.val == q.val &&
                isSame(p.left, q.right) && isSame(p.right, q.left);
    }
}
